{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "# json\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"17623c51f2a3403d9048528e6032cd76\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-1.cognitiveservices.azure.cn//face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3149120088,2228575915&fm=26&gp=0.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'e672aaec-3569-4be6-994b-0f3f01b971ab',\n",
       "  'faceRectangle': {'top': 42, 'left': 107, 'width': 39, 'height': 39},\n",
       "  'faceAttributes': {'smile': 0.014,\n",
       "   'headPose': {'pitch': -12.6, 'roll': -0.9, 'yaw': -3.0},\n",
       "   'gender': 'male',\n",
       "   'age': 31.0,\n",
       "   'facialHair': {'moustache': 0.4, 'beard': 0.4, 'sideburns': 0.4},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.004,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.014,\n",
       "    'neutral': 0.982,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.59},\n",
       "   'noise': {'noiseLevel': 'high', 'value': 1.0},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.81, 'invisible': False, 'hairColor': []}}},\n",
       " {'faceId': 'c89a13f3-146a-41b8-b23f-1df160b82355',\n",
       "  'faceRectangle': {'top': 19, 'left': 238, 'width': 39, 'height': 39},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 0.2, 'roll': -1.6, 'yaw': 15.9},\n",
       "   'gender': 'male',\n",
       "   'age': 30.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.003,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.004,\n",
       "    'fear': 0.005,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.285,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.7},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.51},\n",
       "   'noise': {'noiseLevel': 'high', 'value': 0.96},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.06,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.99},\n",
       "     {'color': 'black', 'confidence': 0.99},\n",
       "     {'color': 'gray', 'confidence': 0.36},\n",
       "     {'color': 'blond', 'confidence': 0.17},\n",
       "     {'color': 'other', 'confidence': 0.07},\n",
       "     {'color': 'red', 'confidence': 0.02}]}}},\n",
       " {'faceId': '62703e0c-e397-48a0-ba0a-c1c8fd99eb1b',\n",
       "  'faceRectangle': {'top': 18, 'left': 360, 'width': 38, 'height': 38},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': 3.8, 'roll': -3.8, 'yaw': 19.9},\n",
       "   'gender': 'male',\n",
       "   'age': 30.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.006,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.001,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.924,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.066},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.45},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.02,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 1.0},\n",
       "     {'color': 'black', 'confidence': 0.7},\n",
       "     {'color': 'gray', 'confidence': 0.29},\n",
       "     {'color': 'blond', 'confidence': 0.28},\n",
       "     {'color': 'red', 'confidence': 0.17},\n",
       "     {'color': 'other', 'confidence': 0.05}]}}}]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#赋值\n",
    "face_all=[{'faceId': 'e672aaec-3569-4be6-994b-0f3f01b971ab',\n",
    "  'faceRectangle': {'top': 42, 'left': 107, 'width': 39, 'height': 39},\n",
    "  'faceAttributes': {'smile': 0.014,\n",
    "   'headPose': {'pitch': -12.6, 'roll': -0.9, 'yaw': -3.0},\n",
    "   'gender': 'male',\n",
    "   'age': 31.0,\n",
    "   'facialHair': {'moustache': 0.4, 'beard': 0.4, 'sideburns': 0.4},\n",
    "   'glasses': 'NoGlasses',\n",
    "   'emotion': {'anger': 0.0,\n",
    "    'contempt': 0.004,\n",
    "    'disgust': 0.0,\n",
    "    'fear': 0.0,\n",
    "    'happiness': 0.014,\n",
    "    'neutral': 0.982,\n",
    "    'sadness': 0.0,\n",
    "    'surprise': 0.0},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
    "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.59},\n",
    "   'noise': {'noiseLevel': 'high', 'value': 1.0},\n",
    "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
    "   'accessories': [],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.81, 'invisible': False, 'hairColor': []}}},\n",
    " {'faceId': 'c89a13f3-146a-41b8-b23f-1df160b82355',\n",
    "  'faceRectangle': {'top': 19, 'left': 238, 'width': 39, 'height': 39},\n",
    "  'faceAttributes': {'smile': 0.0,\n",
    "   'headPose': {'pitch': 0.2, 'roll': -1.6, 'yaw': 15.9},\n",
    "   'gender': 'male',\n",
    "   'age': 30.0,\n",
    "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
    "   'glasses': 'NoGlasses',\n",
    "   'emotion': {'anger': 0.003,\n",
    "    'contempt': 0.001,\n",
    "    'disgust': 0.004,\n",
    "    'fear': 0.005,\n",
    "    'happiness': 0.0,\n",
    "    'neutral': 0.285,\n",
    "    'sadness': 0.001,\n",
    "    'surprise': 0.7},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
    "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.51},\n",
    "   'noise': {'noiseLevel': 'high', 'value': 0.96},\n",
    "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
    "   'accessories': [],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.06,\n",
    "    'invisible': False,\n",
    "    'hairColor': [{'color': 'brown', 'confidence': 0.99},\n",
    "     {'color': 'black', 'confidence': 0.99},\n",
    "     {'color': 'gray', 'confidence': 0.36},\n",
    "     {'color': 'blond', 'confidence': 0.17},\n",
    "     {'color': 'other', 'confidence': 0.07},\n",
    "     {'color': 'red', 'confidence': 0.02}]}}},\n",
    " {'faceId': '62703e0c-e397-48a0-ba0a-c1c8fd99eb1b',\n",
    "  'faceRectangle': {'top': 18, 'left': 360, 'width': 38, 'height': 38},\n",
    "  'faceAttributes': {'smile': 0.0,\n",
    "   'headPose': {'pitch': 3.8, 'roll': -3.8, 'yaw': 19.9},\n",
    "   'gender': 'male',\n",
    "   'age': 30.0,\n",
    "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
    "   'glasses': 'NoGlasses',\n",
    "   'emotion': {'anger': 0.006,\n",
    "    'contempt': 0.001,\n",
    "    'disgust': 0.0,\n",
    "    'fear': 0.001,\n",
    "    'happiness': 0.0,\n",
    "    'neutral': 0.924,\n",
    "    'sadness': 0.001,\n",
    "    'surprise': 0.066},\n",
    "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
    "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.45},\n",
    "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
    "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
    "   'accessories': [],\n",
    "   'occlusion': {'foreheadOccluded': False,\n",
    "    'eyeOccluded': False,\n",
    "    'mouthOccluded': False},\n",
    "   'hair': {'bald': 0.02,\n",
    "    'invisible': False,\n",
    "    'hairColor': [{'color': 'brown', 'confidence': 1.0},\n",
    "     {'color': 'black', 'confidence': 0.7},\n",
    "     {'color': 'gray', 'confidence': 0.29},\n",
    "     {'color': 'blond', 'confidence': 0.28},\n",
    "     {'color': 'red', 'confidence': 0.17},\n",
    "     {'color': 'other', 'confidence': 0.05}]}}}]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建相关调用的数据的空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "glasses = {}\n",
    "smile = {}  \n",
    "hairColor = {}  \n",
    "age = {}  \n",
    "gender = {}  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将人脸的值添加入空字典内"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-27-46d879a3e6d7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mglasses\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Person{}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mface_all\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faceAttributes'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'glasses'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0msmile\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Person{}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mface_all\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faceAttributes'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'smile'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mhairColor\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Person{}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mface_all\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faceAttributes'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'hair'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'hairColor'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'color'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[0mage\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Person{}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mface_all\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faceAttributes'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'age'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0mgender\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Person{}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m\u001b[0mface_all\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faceAttributes'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'gender'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while count < len(face_all):  \n",
    "    glasses[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['glasses']\n",
    "    smile[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['smile']\n",
    "    hairColor[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['hair']['hairColor'][0]['color']\n",
    "    age[\"Person{}\".format(count + 1)] = face_all[count]['faceAttributes']['age']\n",
    "    gender[\"Person{}\".format(count + 1)] =face_all[count]['faceAttributes']['gender']\n",
    "    count += 1  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 打印字典输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄： {}\n",
      "性别： {}\n",
      "眼镜： {'Person1': 'NoGlasses'}\n",
      "微笑指数： {'Person1': 0.014}\n",
      "头发颜色： {}\n"
     ]
    }
   ],
   "source": [
    "print(\"年龄：\", age)\n",
    "print(\"性别：\", gender)  \n",
    "print(\"眼镜：\", glasses)\n",
    "print(\"微笑指数：\", smile)\n",
    "print(\"头发颜色：\", hairColor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
